package cn.interestingshop.service.userAddress;

import cn.interestingshop.dao.UserAddressDao;
import cn.interestingshop.dao.UserAddressDaoImpl;
import cn.interestingshop.entity.UserAddress;
import cn.interestingshop.utils.DataSourceUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserAddressServiceImpl implements UserAddressService {
    @Override
    public List<UserAddress> selectOne(int userId) {

        Connection conn=null;
        List<UserAddress> userAddress=new ArrayList<>();
        try {
            conn= DataSourceUtil.openConnection();
            UserAddressDao userAddressDao=new UserAddressDaoImpl(conn);

            userAddress=userAddressDao.selectOne(userId);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return userAddress;
    }

    @Override
    public int insert(int userId, String address, String remark) {

        Connection conn=null;
        int rs=0;

        try {
            conn=DataSourceUtil.openConnection();
            UserAddressDao userAddressDao=new UserAddressDaoImpl(conn);

            rs=userAddressDao.insert(userId,address,remark);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return rs;
    }

    @Override
    public UserAddress selectOneid(int id) {
        Connection conn=null;
        UserAddress userAddress=new UserAddress();
        try {
            conn= DataSourceUtil.openConnection();
            UserAddressDao userAddressDao=new UserAddressDaoImpl(conn);

            userAddress=userAddressDao.selectOneid(id);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return userAddress;
    }
}
